package com.swmutil.swmutil.sqlite;

/**
 * @author 师文明
 * @data 2024/10/21 下午5:20
 **/

import java.sql.DriverManager;
import java.sql.ResultSet;

public class SQLiteInitConfig {
    static {
        // 加载SQLite驱动程序
        try {
            Class.forName("org.sqlite.JDBC");
            // 创建数据库连接
            ProjectStaticManager.connection = DriverManager.getConnection("jdbc:sqlite:src/main/resources/database.db");

            // 创建Statement对象
            ProjectStaticManager.statement = ProjectStaticManager.connection.createStatement();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        try {
            // 创建表
            String createPageTableSQL = "CREATE TABLE IF NOT EXISTS all_page (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "page_name TEXT)";
            ProjectStaticManager.statement.executeUpdate(createPageTableSQL);
            // 创建表
            String createParamTableSQL = "CREATE TABLE IF NOT EXISTS all_param (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "signal_name TEXT, signal_description TEXT, bit_length INTEGER,param_order INTEGER,value INTEGER,layoutx INTEGER,layouty INTEGER,type INTEGER,options TEXT, unit TEXT,page_id, FOREIGN KEY (page_id) REFERENCES all_page(id))";
            ProjectStaticManager.statement.executeUpdate(createParamTableSQL);


            System.out.println("数据插入成功！");
            // 查询数据
            ResultSet rs = ProjectStaticManager.statement.executeQuery("SELECT * FROM all_param;");
            while (rs.next()) {
                System.out.println("id = " + rs.getString("id"));
            }
            rs.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        // 记得程序结束的时候关闭
        // ProjectStaticManager.statement.close();
        // ProjectStaticManager.connection.close();
    }
}

